<?php
  /**
  * Login Yahoo with OpenID action
  * @author:trungdt
  * @since: Jan 21, 2011
  * @version: 1
  * 
  * Change logs
  * Jan 29, 2011 - trungdt - support OpenID automatic login 
  */
  
if (!defined('EXPONENT')) exit('');  
  // include OpenID Class
  include( BASE."modules/openidmodule/openid.php");
  // check for ajax_action param
  if (!isset($_REQUEST['ajax_action']))
  {
      $_REQUEST['ajax_action']=1;
      $_SERVER['REQUEST_URI'].="&ajax_action=1";
  }
  //
  // redirect if logged in
  if (exponent_users_isLoggedIn())
  {
      exponent_flow_redirect();
  }
  else
  {
      try{
          $openid = @new LightOpenID();
          if(!$openid->mode) {
              $openid->identity = 'yahoo.com';
              //echo $openid->authUrl();
              $openid->required = array(
              'namePerson/friendly',
              'contact/email'       ,
              'namePerson'           ,
              'birthDate'             ,
              'person/gender'          ,
              'contact/postalCode/home' ,
              'contact/country/home'    ,
              'pref/language'          ,
              'pref/timezone'          ,
              'namePerson/first',
              'namePerson/last'
              );
              $openid->optional = array('namePerson/first');
              header('Location: ' . $openid->authUrl());
          }
          else if (($openid->mode == "cancel") || (!$openid->validate()))
          {
              echo "
              <html>
              <body>
              <script language='javascript'>
                 window.opener.login_error(1);
                 window.close();
              </script>
              </body></html>";
          }
          else
          {
                 $temp=$openid->getAttributes();
                 /**
                 * namePerson/friendly: string = "Dang"
                 contact/email: string = "immanuel192@yahoo.com"
                 namePerson: string = "Dang Tin Trung"
                 person/gender: string = "M"
                 pref/language: string = "en-US"
                 pref/timezone: string = "America/New_York"
                 */
                 $myobj=null;
                 $myobj->fist_name="";
                 if (isset ($temp['namePerson'])) $myobj->fist_name=$temp['namePerson'];
                 $myobj->last_name=""; // yahoo does not support last name :)
                 $myobj->email=""; 
                 if (isset ($temp['contact/email'])) $myobj->email=$temp['contact/email'];
                 $myobj->indentifer=$openid->identity;
                 
                 // update into db
                 aboid_updateIdentify($myobj);
                 // save to session
                 $temp=$db->selectObject("user","email = '{$myobj->email}'");
                 if ($temp)
                 {
                     // okies, this user is exist, we login this user
                     $user=$temp;
                     //Update the last login timestamp for this user.
                     $user->last_login = time();
                     $db->updateObject($user, 'user');

                     // Retrieve the full profile, complete with all Extension data.
                     $user = exponent_users_getFullProfile($user);

                     // Call on the Sessions subsystem to log the user into the site.
                     exponent_sessions_login($user);
                 }
                 // now, we redirect parent page
                 echo "<html>
              <body>
              <script language='javascript'>
                 window.opener.document.location='/';
                 window.close();
              </script>
              </body></html>";
          } 
      } catch (ErrorException $e) {
          //echo $e->getMessage();
      }
  }
  //
?>
